import { useState,useEffect } from 'react'


export const useRequest = (requestFn) => {
    const [data,setData] = useState(null)
    const [loading,setLoading] = useState(false)
    const [error,setError] = useState(null)

    const run = async() => {
        try{
            setLoading(true)
            const res = await requestFn()
            setData(res.data)
        }catch(e){
            setError(e)
        }finally{
            setLoading(false)
        }
    }
    useEffect(() => {
        run()
    },[])

    return {
        data,
        loading,
        error,
        run
    }
}